package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ClipboardUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.VibrateUtils;
import com.stark.calculator.general.CalViewModel;
import com.stark.calculator.general.TextBean;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityCalculateBinding;
import flc.ast.utils.TypefaceUtil;
import stark.common.basic.base.BaseActivity;
import stark.common.basic.utils.StatusBarUtils;

public class CalculateActivity extends BaseActivity<CalViewModel, ActivityCalculateBinding> {

    private MediaPlayer mPlayerClick;//点击音效
    private boolean isOpenEffect = true;//是否打开点击音效
    private boolean isOpenShock = true;//是否打开震动

    @Override
    protected int onCreate() {
        //设置状态栏字体颜色
        StatusBarUtils.setStatusBarTranslate(this, View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
        return R.layout.activity_calculate;
    }

    @Override
    protected void initView() {
        mDataBinding.ivCalculateBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivCalculateVoice.setOnClickListener(this);
        mDataBinding.ivCalculateShock.setOnClickListener(this);
        mDataBinding.ivCalculateCopy.setOnClickListener(this);
        mDataBinding.ivCalculateDelete.setOnClickListener(this);

        mDataBinding.ivCalculateClear.setOnClickListener(this);
        mDataBinding.ivCalculateAllClear.setOnClickListener(this);
        mDataBinding.ivCalculateChu.setOnClickListener(this);
        mDataBinding.ivCalculateCheng.setOnClickListener(this);

        mDataBinding.ivCalculateNum7.setOnClickListener(this);
        mDataBinding.ivCalculateNum8.setOnClickListener(this);
        mDataBinding.ivCalculateNum9.setOnClickListener(this);
        mDataBinding.ivCalculateJian.setOnClickListener(this);

        mDataBinding.ivCalculateNum4.setOnClickListener(this);
        mDataBinding.ivCalculateNum5.setOnClickListener(this);
        mDataBinding.ivCalculateNum6.setOnClickListener(this);
        mDataBinding.ivCalculateJia.setOnClickListener(this);

        mDataBinding.ivCalculateNum1.setOnClickListener(this);
        mDataBinding.ivCalculateNum2.setOnClickListener(this);
        mDataBinding.ivCalculateNum3.setOnClickListener(this);
        mDataBinding.ivCalculateNum0.setOnClickListener(this);
        mDataBinding.ivCalculateDian.setOnClickListener(this);
        mDataBinding.ivCalculateDy.setOnClickListener(this);
        TypefaceUtil.setViewTypefaceBySdc(mDataBinding.tvCalculateRecord, mDataBinding.tvCalculateInput, mDataBinding.tvCalculateResult);
    }

    @Override
    protected void onClickCallback(View v) {
        if (isOpenEffect) {
            openClick();
        }
        if (isOpenShock) {
            VibrateUtils.vibrate(200);
        }
        switch (v.getId()) {
            case R.id.ivCalculateVoice:
                isOpenEffect = !isOpenEffect;
                if (isOpenEffect) {
                    mDataBinding.ivCalculateVoice.setImageResource(R.drawable.syin1);
                } else {
                    mDataBinding.ivCalculateVoice.setImageResource(R.drawable.syin2);
                }
                break;
            case R.id.ivCalculateShock:
                isOpenShock = !isOpenShock;
                if (isOpenShock) {
                    mDataBinding.ivCalculateShock.setImageResource(R.drawable.zdong1);
                } else {
                    mDataBinding.ivCalculateShock.setImageResource(R.drawable.zdong2);
                }
                break;
            case R.id.ivCalculateCopy:
                String result = mDataBinding.tvCalculateResult.getText().toString().trim();
                if (TextUtils.isEmpty(result) || result.equals("=")) return;
                ClipboardUtils.copyText(result.replace("=", ""));
                ToastUtils.showShort(R.string.copy_result);
                break;
            case R.id.ivCalculateDelete:
                mViewModel.handleClickDelete();
                break;
            case R.id.ivCalculateAllClear:
            case R.id.ivCalculateClear:
                mViewModel.clears();
                break;
            case R.id.ivCalculateChu:
                mViewModel.handleClickAsmd("÷");
                break;
            case R.id.ivCalculateCheng:
                mViewModel.handleClickAsmd("×");
                break;
            case R.id.ivCalculateNum7:
                mViewModel.handleClickNum("7");
                break;
            case R.id.ivCalculateNum8:
                mViewModel.handleClickNum("8");
                break;
            case R.id.ivCalculateNum9:
                mViewModel.handleClickNum("9");
                break;
            case R.id.ivCalculateJian:
                mViewModel.handleClickAsmd("-");
                break;
            case R.id.ivCalculateNum4:
                mViewModel.handleClickNum("4");
                break;
            case R.id.ivCalculateNum5:
                mViewModel.handleClickNum("5");
                break;
            case R.id.ivCalculateNum6:
                mViewModel.handleClickNum("6");
                break;
            case R.id.ivCalculateJia:
                mViewModel.handleClickAsmd("+");
                break;
            case R.id.ivCalculateNum0:
                mViewModel.handleClickNum("0");
                break;
            case R.id.ivCalculateNum1:
                mViewModel.handleClickNum("1");
                break;
            case R.id.ivCalculateNum2:
                mViewModel.handleClickNum("2");
                break;
            case R.id.ivCalculateNum3:
                mViewModel.handleClickNum("3");
                break;
            case R.id.ivCalculateDian:
                mViewModel.handleClickPoint(".");
                break;
            case R.id.ivCalculateDy:
                mViewModel.handleClickEqual();
                break;
        }
    }

    @Override
    protected void initData() {
        //初始化点击音效
        mPlayerClick = MediaPlayer.create(mContext, R.raw.music_click);
        mPlayerClick.setLooping(false);//循环播放
        // 中间计算的步骤
        mViewModel.getFuncText().observe(this, textBean -> {
            updateViewText(mDataBinding.tvCalculateInput, textBean);
        });
        // 最后得到的结果
        mViewModel.getEqualText().observe(this, textBean -> {
            updateViewText(mDataBinding.tvCalculateResult, textBean);
        });
        // 历史记录
        mViewModel.getHistoryText().observe(this, textBean -> {
            updateViewText(mDataBinding.tvCalculateRecord, textBean);
        });
    }

    private void updateViewText(TextView textView, TextBean textBean) {
        if (textView == null || textBean == null) {
            return;
        }
        textView.setText(textBean.getText());
        if (textBean.getTextSize() > 0) {
            textView.setTextSize(textBean.getTextSize());
        }
    }

    @Override
    protected CalViewModel initViewModel() {
        return new ViewModelProvider(this).get(CalViewModel.class);
    }

    @Override
    protected void showError(Object obj) {

    }

    //选中音效
    private void openClick() {
        if (mPlayerClick.isPlaying()) return;
        mPlayerClick.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        VibrateUtils.cancel();
        //释放点击音效播放器
        if (mPlayerClick != null) {
            mPlayerClick.stop();
            mPlayerClick.release();
        }
    }

}